package cn.yunyichina.provider.hymed.service.impl;

import cn.yunyichina.provider.framework.annotation.validate.ValidateException;
import cn.yunyichina.provider.framework.annotation.validate.Validator;
import cn.yunyichina.provider.framework.common.ResponseEnum;
import cn.yunyichina.provider.framework.dubbo.service.BaseService;
import cn.yunyichina.provider.hymed.constant.NeusoftFunctionId;
import cn.yunyichina.provider.hymed.model.bo.MedicalCardBindRequestBO;
import cn.yunyichina.provider.hymed.model.bo.MedicalCardBindResponseBO;
import cn.yunyichina.provider.hymed.model.domain.HymedPaymentSettingDO;
import cn.yunyichina.provider.hymed.model.vo.UserBindCardRequestVo;
import cn.yunyichina.provider.hymed.service.neusoft.NeusoftBaseService;
import cn.yunyichina.provider.hymed.service.neusoft.NeusoftService;
import cn.yunyichina.provider.iface.entity.base.PersonalUserFamilyDto;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.utils.convert.JsonUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Administrator on 2017/10/26.
 */
@Service
public class CardServiceImpl extends NeusoftBaseService {


    private Logger logger = LoggerFactory.getLogger(CardServiceImpl.class);


    private final NeusoftService neusoftService;

    @Autowired
    public CardServiceImpl(NeusoftService neusoftService) {
        this.neusoftService = neusoftService;
    }

    /**
     * 一卡通用户社保金融卡绑定
     * yyt.hymed.card.insurance.bind
     * @param data
     * @param sessionId
     * @return
     */
    public Response bindCardInsurance(String data, String sessionId) {
        Response response = new Response();
        logger.info("一卡通社保金融卡绑定 --->method:bindCardInsurance,data:" + data + "！");
        UserBindCardRequestVo requestVo = JsonUtils.parseObject(data, UserBindCardRequestVo.class);

        // 校验RequestVo的数据h
        try {
            Validator.getInstance().validate(requestVo);
        } catch (ValidateException ex) {
            logger.info(" 数据校验失败:" + ex.getMessage() + "|入参:" + data + "！");
            logger.error("error", ex);
            response.setResultCodeAndMessage("24001", "数据校验失败:" + ex.getMessage() + "！");
            return response;
        }
        //-----------------------------------------------------------------------------------------------------

//        PersonalUserFamilyDto familyDto = BaseService.getBaseClient().getPersonalUserFamily(requestVo.getFamilyId());
//        if(familyDto == null){
//            response.setResultCodeAndMessage("24002", "家人数据不存在！");
//            return response;
//        }
//        if(StringUtils.isBlank(familyDto.getInsuranceLoginName())){
//            response.setResultCodeAndMessage("24003", "请先登录或注册！");
//            return response;
//        }
//        if(1 != familyDto.getRealNameFlag()){
//            response.setResultCodeAndMessage("24004", "请先实名验证！");
//            return response;
//        }

        //调用4.1.19绑定金融社保卡接口
        MedicalCardBindRequestBO requestBO = new MedicalCardBindRequestBO();
        requestBO.setFunction_id(NeusoftFunctionId.BIND_MEDICAL_CARD);
        HymedPaymentSettingDO hymedPaymentSettingDO=getNeusoftSessionId(requestVo);
        requestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
        requestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
        requestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());

//        requestBO.setPortalLoginId(familyDto.getInsuranceLoginName());
        requestBO.setPortalLoginId(requestVo.getLoginName());
        requestBO.setFull_name(requestVo.getPatName());
        requestBO.setId_number(requestVo.getPatIdNo());
        //社保卡区域 河源填写 4416
        requestBO.setCity(requestVo.getCity());
        requestBO.setSicard_account(requestVo.getInsuranceCardNo());
        requestBO.setBank_account(requestVo.getBankCardNo());
        requestBO.setPdtype("1");
        requestBO.setPassword01(requestVo.getPayPassword());
        requestBO.setPassword02(requestVo.getPayPassword());
        requestBO.setOldpassword("");
        MedicalCardBindResponseBO responseBO = neusoftService.doExecute(requestBO, MedicalCardBindResponseBO.class);

        if (responseBO == null || !responseBO.getBizSuccess()) {
            response.setResultCodeAndMessage(ResponseEnum.Common.FAILURE.getCode(),
                    (responseBO != null && StringUtils.isNotBlank(responseBO.getMessage())) ? responseBO.getMessage() : "调用社保金融卡绑定接口失败");
            return response;
        }else{
//            familyDto.setBindInsuranceCardFlag(1);
            response.setResultCodeAndMessage("0","绑卡成功");
        }

        return response;
    }
}
